package com.jcark.ui.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

public class ThermometerView extends View {
    private Paint paint, mercuryPaint;
    private RectF thermometerBody;
    private float minTemp = 35;
    private float maxTemp = 42;
    private float currentTemp = 36.6f;
    private ValueAnimator animator;
    private int[] temperatureColors = {
            Color.BLUE,    // 35°C
            Color.CYAN,     // 36°C
            Color.GREEN,    // 37°C
            Color.YELLOW,   // 38°C
            Color.rgb(255, 165, 0), // 39°C (橙色)
            Color.RED       // 40°C+
    };

    public ThermometerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        // 体温计主体画笔
        paint = new Paint();
        paint.setColor(Color.DKGRAY);
        paint.setStrokeWidth(5);
        paint.setStyle(Paint.Style.STROKE);

        // 水银柱画笔
        mercuryPaint = new Paint();
        mercuryPaint.setStyle(Paint.Style.FILL);

        thermometerBody = new RectF();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth();
        int height = getHeight();

        // 绘制体温计主体
        float bodyWidth = width * 0.2f;
        thermometerBody.set(
                (width - bodyWidth) / 2, 50,
                (width + bodyWidth) / 2, height - 50
        );
        canvas.drawRoundRect(thermometerBody, 30, 30, paint);

        // 计算刻度位置
        float scaleHeight = thermometerBody.height();
        float tempRange = maxTemp - minTemp;
        float scaleStep = scaleHeight / tempRange;

        // 绘制刻度线和数字
        paint.setTextSize(30);
        paint.setTextAlign(Paint.Align.LEFT);
        for (float temp = minTemp; temp <= maxTemp; temp += 0.5f) {
            float y = thermometerBody.bottom - (temp - minTemp) * scaleStep;
            if (temp % 1 == 0) {
                canvas.drawText(String.valueOf((int) temp), thermometerBody.right + 20, y + 10, paint);
                canvas.drawLine(thermometerBody.right, y, thermometerBody.right + 15, y, paint);
            } else {
                canvas.drawLine(thermometerBody.right, y, thermometerBody.right + 10, y, paint);
            }
        }

        // 计算水银柱高度和颜色
        float mercuryHeight = (currentTemp - minTemp) * scaleStep;
        float mercuryTop = thermometerBody.bottom - mercuryHeight;
        RectF mercuryRect = new RectF(
                thermometerBody.left + 10,
                mercuryTop,
                thermometerBody.right - 10,
                thermometerBody.bottom - 10
        );

        // 设置渐变颜色
        float colorPosition = (currentTemp - minTemp) / tempRange;
        int colorIndex = (int) (colorPosition * (temperatureColors.length - 1));
        colorIndex = Math.min(Math.max(colorIndex, 0), temperatureColors.length - 2);

        LinearGradient gradient = new LinearGradient(
                0, mercuryRect.top,
                0, mercuryRect.bottom,
                new int[]{temperatureColors[colorIndex], temperatureColors[colorIndex + 1]},
                null,
                Shader.TileMode.CLAMP
        );
        mercuryPaint.setShader(gradient);

        // 绘制水银柱
        canvas.drawRoundRect(mercuryRect, 15, 15, mercuryPaint);
    }

    public void setCurrentTemp(float targetTemp) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            if (animator != null && animator.isRunning()) {
                animator.cancel();
            }

            animator = ValueAnimator.ofFloat(currentTemp, targetTemp);
            animator.setDuration(1000);
            animator.addUpdateListener(animation -> {
                currentTemp = (float) animation.getAnimatedValue();
                invalidate();
            });
            animator.start();
        } else {
            //King.zi todo
        }
    }

    // 添加温度单位切换
    public void switchUnit(boolean isCelsius) {
        minTemp = isCelsius ? 35 : 95;  // 转换为华氏度范围
        maxTemp = isCelsius ? 42 : 107.6f;
        invalidate();
    }
}
